package com.yunchao.bank.configure.presentation.screen

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.lifecycle.lifecycleScope
import by.kirich1409.viewbindingdelegate.viewBinding
import com.yunchao.bank.databinding.ActivityConfigureBinding
import com.yunchao.feature.base.presentation.activity.BaseActivity
import com.yunchao.bank.R
import com.yunchao.bank.configure.domain.model.ConfigureModel
import com.yunchao.bank.main.presentation.screen.MainActivity
import com.yunchao.feature.base.presentation.ext.hideLoadingDialog
import com.yunchao.feature.base.presentation.ext.setDebouncedClickListener
import com.yunchao.feature.base.presentation.ext.showLoadingDialog
import com.yunchao.feature.base.presentation.ext.toast
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.viewModel

internal class ConfigureActivity : BaseActivity(R.layout.activity_configure) {

    private val binding: ActivityConfigureBinding by viewBinding()
    private val viewModel: ConfigureViewModel by viewModel<ConfigureViewModel>()

    private val isShowBack: Boolean by lazy { intent.getBooleanExtra(IS_SHOW_BACK_KEY, false) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setSupportActionBar(binding.baseToolbar)
        supportActionBar?.apply {
            setDisplayHomeAsUpEnabled(isShowBack)
//            title = getString(com.yunchao.feature.base.R.string.环境设置)
        }

        binding.activitySetBtn.setDebouncedClickListener {
            viewModel.saveConfigure(
                ConfigureModel(
                    serverIp = binding.inputServerIp.centerEditValue,
                    serverPort = binding.inputServerPort.centerEditValue,
                    singlePower = binding.inputSinglePower.centerEditValue.toInt(),
                    loopPower = binding.inputLoopPower.centerEditValue.toInt(),
                    deviceNumber = binding.inputDeviceNumber.centerEditValue,
                    deviceId = binding.inputDeviceId.centerEditValue,
                    pdaDeviceIp = binding.inputPDAIp.centerEditValue,
                    pdaDevicePort = binding.inputPDAPort.centerEditValue,
                    personNumber = binding.inputPersonNumber.centerEditValue,
                    icCardIp = binding.inputIcCardIp.centerEditValue,
                    icCardPort = binding.inputIcCardPort.centerEditValue,
                )
            )
        }

        lifecycleScope.launch { viewModel.uiStateFlow.collect(::render) }
        lifecycleScope.launch { viewModel.uiEffectFlow.collect(::sideEffect) }
    }

    private fun render(state: ConfigureViewModel.UiState) {
        when (state) {
            is ConfigureViewModel.UiState.Idle -> Unit
            is ConfigureViewModel.UiState.Success -> {
                binding.inputServerIp.setCenterEditString(state.configure.serverIp)
                binding.inputServerPort.setCenterEditString(state.configure.serverPort)
                binding.inputSinglePower.setCenterEditString(state.configure.singlePower.toString())
                binding.inputLoopPower.setCenterEditString(state.configure.loopPower.toString())
                binding.inputDeviceNumber.setCenterEditString(state.configure.deviceNumber)
                binding.inputDeviceId.setCenterEditString(state.configure.deviceId)
                binding.inputPDAIp.setCenterEditString(state.configure.pdaDeviceIp)
                binding.inputPDAPort.setCenterEditString(state.configure.pdaDevicePort)
                binding.inputPersonNumber.setCenterEditString(state.configure.personNumber)
                binding.inputIcCardIp.setCenterEditString(state.configure.icCardIp)
                binding.inputIcCardPort.setCenterEditString(state.configure.icCardPort)
            }
        }
    }

    private fun sideEffect(effect: ConfigureViewModel.UiEffect) {
        when (effect) {
            is ConfigureViewModel.UiEffect.ShowLoading -> showLoadingDialog()
            is ConfigureViewModel.UiEffect.HideLoading -> hideLoadingDialog()
            is ConfigureViewModel.UiEffect.ShowToast -> toast(effect.msg)
            is ConfigureViewModel.UiEffect.NavigateToMain -> {
                if (!isShowBack) {
                    startActivity(Intent(this@ConfigureActivity, MainActivity::class.java))
                }
                finish()
            }
        }
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                // 处理返回按钮点击事件
                onBackPressedDispatcher.onBackPressed()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

    companion object {
        const val IS_SHOW_BACK_KEY = "IS_SHOW_BACK_KEY"

        fun openConfigure(context: Activity, isShowBack: Boolean = false) {
            context.startActivity(
                Intent(context, ConfigureActivity::class.java).apply {
                    putExtra(IS_SHOW_BACK_KEY, isShowBack)
                }
            )
        }
    }
}